package org.zbclan.bf2.rcon.home.client.ui;

import java.util.ArrayList;

import org.zbclan.bf2.rcon.common.client.CommonBundle;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class HomeView extends Composite implements HomeDisplay {

	private static HomeViewUiBinder uiBinder = GWT
			.create(HomeViewUiBinder.class);

	interface HomeViewUiBinder extends UiBinder<Widget, HomeView> {
	}

	public static final String GPO_PREFIX = "GameModes/";
	public static final String GPO_SUFFIX = "/GamePlayObjects.con";

	private HasClickHandlers[] levelNameClickables;
	private HasClickHandlers[] gameModeClickables;

	private DialogBox gameModeDialog;

	@UiField
	HasWidgets container;

	@UiField
	Panel gameModesContainer;

	public HomeView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public Widget asWidget() {
		return this;
	}

	public void startProcessing() {
	}

	public void stopProcessing() {
	}

	public void showLevelNameList(String[] names) {

		ArrayList<HasClickHandlers> lst = new ArrayList<HasClickHandlers>();

		container.clear();

		for (String name : names) {
			final Label w = new Label(name);
			w.addMouseOverHandler(new MouseOverHandler() {

				public void onMouseOver(MouseOverEvent event) {
					w.addStyleName(CommonBundle.INSTANCE.commonStyle()
							.highlight());
				}
			});
			w.addMouseOutHandler(new MouseOutHandler() {

				public void onMouseOut(MouseOutEvent event) {
					w.removeStyleName(CommonBundle.INSTANCE.commonStyle()
							.highlight());
				}
			});
			container.add(w);
			lst.add(w);
		}

		levelNameClickables = lst.toArray(new HasClickHandlers[names.length]);

		return;
	}

	public void showGameModeListDialog(String name) {

		gameModeDialog = new DialogBox();
		gameModeDialog.setText("select size for level " + name);
		gameModeDialog.setWidget(gameModesContainer);
		gameModeDialog.setModal(true);
		gameModeDialog.setAutoHideEnabled(true);
		gameModeDialog.setAnimationEnabled(true);
		gameModeDialog.setGlassEnabled(true);
		gameModeDialog.show();

		return;
	}

	public void updateSizeSelectionDialog(String[] modes) {

		gameModesContainer.clear();

		// levelSizeSeletionDialog.setWidget(new Label(Arrays.toString(sizes)));
		ArrayList<HasClickHandlers> lst = new ArrayList<HasClickHandlers>();

		for (String mode : modes) {
			String m = mode.substring(GPO_PREFIX.length(), mode.length()
					- GPO_SUFFIX.length());
			Label w = new Label(m);
			gameModesContainer.add(w);
			lst.add(w);
		}

		gameModeClickables = lst.toArray(new HasClickHandlers[modes.length]);

		return;
	}

	public HasClickHandlers[] getLevelNameClickables() {
		return levelNameClickables;
	}

	public HasWidgets getLevelNamesContainer() {
		return container;
	}

	public HasClickHandlers[] getGameModeClickables() {
		return gameModeClickables;
	}

	public void hideGameModeDialog() {
		gameModeDialog.hide();
	}

}
